﻿using Microsoft.EntityFrameworkCore;
using SmartConference.Infrastructure.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace SmartConference.Infrastructure.Implements
{
    /// <summary>
    /// 仓储基类实现
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BaseRepository<T> : IBaseRepository<T> where T : class
    {
        private readonly SmartConferenceDbContext db;

        public BaseRepository(SmartConferenceDbContext db)
        {
            this.db = db;
        }
        public virtual async Task<int> AddAsync(T entity)
        {
            try
            {
                await db.Set<T>().AddAsync(entity);
                return await db.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public virtual async Task<int> AddRangeAsync(IList<T> list)
        {
            try
            {
                await db.Set<T>().AddRangeAsync(list);
                return await db.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public async Task<int> DeleteAsync(T entity)
        {
            try
            {
               
                db.Set<T>().Remove(entity);
                return await db.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public virtual async Task<int> DeleteRange(IList<T> list)
        {
            try
            {
                db.Set<T>().RemoveRange(list);
                return await db.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public virtual IQueryable<T> GetAll()
        {
            try
            {
                return db.Set<T>().AsQueryable();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public virtual IQueryable<T> GetAll(Expression<Func<T, bool>> expression)
        {
            try
            {
                return db.Set<T>().Where(expression).AsQueryable();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public virtual async Task<T> GetAsync(Expression<Func<T, bool>> expression)
        {
            try
            {
                return await db.Set<T>().Where(expression).FirstOrDefaultAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public virtual async Task<int> UpdateAsync(T entity)
        {
            try
            {
                db.Set<T>().Update(entity);
                return await db.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public virtual async Task<int> UpdateRangeAsync(IList<T> list)
        {
            try
            {
                db.Set<T>().UpdateRange(list);
                return await db.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
